<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '40px',
    }"
  >
    <div class="wrap-other-roll">
      <el-form
        ref="formSearch"
        label-width="100px"
        label-position="left"
        size="medium"
        ><div class="wrap-other-roll-launch">
          <el-form-item label="工单类型" required>
            <el-cascader
              style="width: 100%"
              v-model="formTypeId"
              :options="typeData"
              @change="handleChange"
              :props="{
                value: 'id',
                label: 'typeName',
                children: 'childList',
                expandTrigger: 'hover',
              }"
              :checkStrictly="true"
              :show-all-levels="false"
              placeholder="请选择工单类型"
              ref="typeCascader"
              clearable
              popper-class="theder"
            >
            </el-cascader>
          </el-form-item>
        </div>
        <div class="wrap-other-roll-launch">
          <el-form-item label="工单名称" required>
            <el-select
              clearable
              v-model="name"
              placeholder="请选择工单名称"
              style="width: 100%"
              @change="formList"
            >
              <el-option
                v-for="item in title"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="wrap-other-roll-launch">
          <el-form-item label="工单标题" required>
            <el-input
              v-model.trim="titles"
              placeholder="请输入工单标题"
              maxlength="50"
              clearable
              style="width: 100%"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <el-empty v-if="formItem.length == 0" :image-size="200"></el-empty>
      <div v-else>
        <handle-form ref="form" :form="formConf" :formItem="formItem" />
        <span
          class="wrap-btn wrap-success"
          style="
            width: 70px;
            text-align: center;
            margin: 0 auto;
            display: block;
          "
          v-if="$checkPermission(101)"
          @click="save"
          >提交</span
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeData: [],
      title: [],
      formTypeId: '',
      name: '',
      nameId: '',
      titles: '',
      formConf: {},
      formItem: [],
      processContent: [],
    }
  },
  created() {
    this.typeList()
  },
  methods: {
    itemList(val) {
      // 获取列表
      this.$axios
        .post('/workorder/setting/pageLaunch', {
          pageNum: 1,
          pageSize: 100000,
          param: { formTypeId: val },
        })
        .then((data) => {
          this.title = []
          if (data.code == 200) {
            this.title = data.data.list
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    formList(val) {
      //获取表单
      this.nameId = val
      this.titles = ''
      this.$axios.get('/workorder/setting/find?id=' + val).then((data) => {
        this.formConf = {}
        this.formItem = []
        this.processContent = []
        if (data.code == 200) {
          this.formConf = JSON.parse(data.data.form)
          this.formItem = JSON.parse(data.data.formItem)
          this.processContent = JSON.parse(data.data.processContent)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    save() {
      //保存
      if (!this.titles) {
        this.$message.error('请输入工单标题')
        return
      }
      const form = this.$refs.form.save()
      if (!form) return
      this.$axios
        .post('/workorder/workOrder/workOrderLnitiateSave', {
          form: JSON.stringify(form.form),
          formItem: JSON.stringify(form.formItem),
          nameId: this.nameId,
          processContent: JSON.stringify(this.processContent),
          title: this.titles,
          source: this.source,
        })
        .then((data) => {
          if (data.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
            })
            location.reload()
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    typeList() {
      // 获取类型
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/workTypeList')
        .then((data) => {
          this.typeData = []
          if (data.code == 200) {
            this.typeData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    handleChange(val) {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
      let id = val ? val[val.length - 1] : ''
      this.itemList(id)
      if (val) {
        this.formTypeId = val[val.length - 1]
      } else {
        this.formTypeId = ''
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.wrap-other-roll {
  width: 888px;
  margin: 20px auto;
  &-launch {
    padding: 0 10px;
  }
}
</style>
